/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.pojo.PPOutBoundInvertoryUI;
import me.zhengjie.pojo.sqlServer.PPInfo;
import me.zhengjie.pojo.sqlServer.PPInventory;
import me.zhengjie.service.PPInfoService;
import me.zhengjie.service.dto.PPInfoQueryCriteria;
import me.zhengjie.service.dto.PPInventoryDto;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author xinglin
 * @website https://eladmin.vip
 * @date 2024-08-03
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "PP信息管理")
@RequestMapping("/api/ppInfo")
public class PPInfoController {

    private final PPInfoService ppInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ppInfo:list')")
    public void exportPpInfo(HttpServletResponse response, PPInfoQueryCriteria criteria) throws IOException {
        ppInfoService.download(ppInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询库存中的PPInfo")
    @ApiOperation("查询库存中的PPInfo")
    @PreAuthorize("@el.check('ppInfo:list')")
//    public ResponseEntity<PageResult<PPInfoDto>> queryPpInfo(PPInfoQueryCriteria criteria, Pageable pageable){
//        return new ResponseEntity<>(ppInfoService.queryAll(criteria,pageable),HttpStatus.OK);
//    }
//    public ResponseEntity<PageResult<PPOutBoundInvertoryUI>> queryPpInfo(PPInfoQueryCriteria criteria, Pageable pageable){
//        return new ResponseEntity<>(ppInfoService.queryAll(criteria,pageable),HttpStatus.OK);
//    }
    public ResponseEntity<PageResult<PPInventoryDto>> joinInventory(PPInfoQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(ppInfoService.queryAllbyjoin(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增PPInfo")
    @ApiOperation("新增PPInfo")
    @PreAuthorize("@el.check('ppInfo:add')")
    public ResponseEntity<Object> createPpInfo(@Validated @RequestBody PPInfo resources) {
        ppInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改PPInfo")
    @ApiOperation("修改PPInfo")
    @PreAuthorize("@el.check('ppInfo:edit')")
    public ResponseEntity<Object> updatePpInfo(@Validated @RequestBody PPInfo resources) {
        ppInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除PPInfo")
    @ApiOperation("删除PPInfo")
    @PreAuthorize("@el.check('ppInfo:del')")
    public ResponseEntity<Object> deletePpInfo(@RequestBody Integer[] ids) {
        ppInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


//    @GetMapping("/queryPpInfoById/{id}")
//    @Log("通过id查询PPInfo")
//    @ApiOperation("通过id查询PPInfo")
//    @PreAuthorize("@el.check('ppInfo:list')")
//    public PPInfo queryPpInfoById(@PathVariable Integer id){
//       return  ppInfoService.queryById(id);
//
//    }

//    //连表查询
//    @GetMapping("/joinInventory")
//    @Log("连表查询")
//    @ApiOperation("连表查询")
//    @PreAuthorize("@el.check('ppInfo:list')")
//    public ResponseEntity<PageResult<PPInventoryDto>> joinInventory(PPInfoQueryCriteria criteria, Pageable pageable){
//        return new ResponseEntity<>(ppInfoService.queryAllbyjoin(criteria,pageable),HttpStatus.OK);
//    }


    @GetMapping("/queryPpInfoById/{palletCode}")
    @Log("通过托盘码查询PPInfo")
    @ApiOperation("通过托盘码查询PPInfo")
//    @PreAuthorize("@el.check('ppInfo:list')")
    public PPInfo queryPpInfoById(@PathVariable String palletCode) {
        return ppInfoService.queryByPalletCode(palletCode);
    }


}